Bee.data.DWRStore = function(config){
    Ext.apply(this, config);
    if (!this.proxy && this.dwr) {
        this.dwr.ownerCt = config.ownerCt;
        this.proxy = new Bee.data.DWRProxy(this.dwr);
    }
    if (!this.reader) {
        this.reader = new Bee.data.DWRReader(this.fields || ['key', 'label'], this.defaultItems ? {
            defaultItems: this.defaultItems
        } : null);
    }
    this.addEvents({
        beforesave: true,
        save: true,
        saveexception: true
    });
    Bee.data.DWRStore.superclass.constructor.call(this);
};

Ext.extend(Bee.data.DWRStore, Ext.data.Store, {
    save: function(options){
        options = options ? options : {};
        if (this.fireEvent("beforesave", this, options) !== false) {
            this.storeOptions(options);
            var p = Ext.apply(options.params ? options.params : {}, this.baseParams);
            this.proxy.save(p, this.saveRecords, this, options);
        }
    },
    saveRecords: function(o, options, success){
        if (!o || success === false) {
            if (success !== false) {
                this.fireEvent("save", this, {}, options);
            }
            if (options.callback) {
                options.callback.call(options.scope || this, [], options, false);
            }
            return;
        }
        this.data.clear();
        this.data.add(o);
        this.totalLength = 1;
        this.fireEvent("save", this, o, options);
        if (options.callback) {
            options.callback.call(options.scope || this, o, options, true);
        }
    },
    update: function(options){
        options = options ? options : {};
        if (this.fireEvent("beforesave", this, options) !== false) {
            this.storeOptions(options);
            var p = Ext.apply(options.params ? options.params : {}, this.baseParams);
            this.proxy.update(p, this.updateRecords, this, options);
        }
    },
    updateRecords: function(o, options, success){
        if (!o || success === false) {
            if (success !== false) {
                this.fireEvent("save", this, {}, options);
            }
            if (options.callback) {
                options.callback.call(options.scope || this, [], options, false);
            }
            return;
        }
        this.data.clear();
        this.data.add(o);
        this.totalLength = 1;
        this.fireEvent("save", this, o, options);
        if (options.callback) {
            options.callback.call(options.scope || this, o, options, true);
        }
    }
});
